// pages/home/home.js
import { showText } from '../../utils/util'
import { consult, type_list } from '@/api/spaceDetail'
import { getgemomemberuser } from '../../api/my'
Page({
  /**
   * 页面的初始数据
   */
  data: {
    imgUri: getApp().globalData.imgUri,
    bindRegionChange: [],
    province: '',
    city: '',
    area: '',
    brand: '',
    realname: '',
    mobile: '',
    brandArray: [
      {brandName:"法儿曼",checked:false},
      {brandName:"悦碧施",checked:false},
      {brandName:"菲欧曼",checked:false},
      {brandName:"施维兰",checked:false},
      {brandName:"其他",checked:false}
    ],
    showDownBox: false,
    
    brandEleBottom: 0,
  },
  onLoad(options) {
    getgemomemberuser({}).then((res) => {
      if (res.code == 200) {
        let name = res.data.familyName && res.data.givenName
        this.setData({
          name: name ? res.data.familyName + res.data.givenName : '',
          mobile: res.data.phonenumber,
        })
      }
    });
    /*
    type_list({}).then((res) => {
      if (res.code == 200) {
        let list = res.rows.map((r) => {
          r.checked = false
          return r
        })
        this.setData({
          brandArray: list,
        })
      }
    })

     */

    setTimeout(() => {
      // 获取当前页面的高度
      const windowInfo = wx.getWindowInfo()
      // 获取id=brand 的元素的位置
      const query = wx.createSelectorQuery()
      query.select('#brand').boundingClientRect()
      query.exec((res) => {
        console.log(res)
        console.log(windowInfo.screenHeight)
        this.setData({
          //brandEleBottom: windowInfo.screenHeight - res[0].bottom,
          brandEleBottom: res[0].top-10,
        });
      });
    }, 100)
  },

  closeDownBox(){
    this.setData({
      showDownBox: false,
    })
  },
  openDownBox() {
    this.setData({
      showDownBox: true,
    })
  },

  bindRegionChange(e) {
    let { code, value } = e.detail
    console.log(value)
    this.setData({
      province: value[0],
      city: value[1],
      area: value[2],
    })
  },
  bindbrandChange(e) {
    let { index } = e.currentTarget.dataset
    let list = this.data.brandArray
    list[index].checked = !list[index].checked
    this.setData({
      brandArray: list,
    },()=>{
      this.setBrandName()
    })
  },
  setBrandName(){
    let arr = this.data.brandArray.filter(r=>r.checked).map(r=>r.brandName)
    console.log(arr)
    this.setData({
      brand:arr.join(',')
    })
  },
  formSubmit(e) {
    let form = e.detail.value
    if (!form.name) {
      showText('请输入姓名')
      return
    }
    if (!form.phone) {
      showText('请输入电话')
      return
    }
    if (!form.region) {
      showText('请选择地区')
      return
    }
    if (!form.storeAddress) {
      showText('请输入门店地址')
      return
    }
    if (!form.storeName) {
      showText('请输入门店名称')
      return
    }
    if (!form.storeNum) {
      showText('请输入门店数量')
      return
    }
    if (!form.storeArea) {
      showText('请输入主店面积')
      return
    }
    if (!form.storeYear) {
      showText('请输入经营年数')
      return
    }
    if (!this.data.brand) {
      showText('请选择品牌')
      return
    }

    console.log(form)
    let params = {
      bizYear: form.storeYear, //经营年数
      city: form.region[1], //城市
      district: form.region[2], //区域
      mainShopScale: form.storeArea, //主店面积
      name: form.name, //姓名
      province: form.region[0], //省份
      shopAddr: form.storeAddress, //门店地址
      shopName: form.storeName, //门店名称
      shopNum: form.storeNum, //门店数量
      tel: form.phone, //电话
      currentBrands: this.data.brand, //品牌
    }

    consult(params).then((res) => {
      if (res.code == 200) {
        wx.showModal({
          title: '提示',
          content: '提交成功',
          showCancel: false,
          success: function (res) {
            wx.navigateBack({
              delta: 1,
            })
          },
        })
      } else {
        wx.showModal({
          title: '提示',
          content: res.msg,
          showCancel: false,
        })
      }
    })
  },
  onShareAppMessage() {
    return {
      title: getApp().globalData.share_title,
      path: getApp().globalData.share_url,
      imageUrl: getApp().globalData.share_image,
    }
  }
})
